package com.leaves.leetcode.array.t1052_MaxSatisfied;


/**
 * 核心要点：
**/
 class Solution {
    /**
     * 固定长度滑动窗口
     * 时间复杂度：O()
     * 空间复杂度：O()
     * @param customers
     * @param grumpy
     * @param minutes
     * @return
     */
    public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
        int n = customers.length;
        int ans = 0, maxChangeSum = Integer.MIN_VALUE, subChangeSum = 0;
        for (int i = 0; i < minutes; i++) {
            subChangeSum += grumpy[i] * customers[i];
            ans += grumpy[i] == 0 ? customers[i] : 0;
        }
        maxChangeSum = Math.max(maxChangeSum, subChangeSum);
        for (int i = minutes; i < n; i++) {
            subChangeSum += grumpy[i] * customers[i] - grumpy[i - minutes] * customers[i - minutes];
            maxChangeSum = Math.max(maxChangeSum, subChangeSum);
            ans += grumpy[i] == 0 ? customers[i] : 0;
        }

        return ans + maxChangeSum;
    }
 }